#include "framemodel.h"

const int FrameModel::MAX_COLUMN_NUM = 1;

FrameModel::FrameModel(AnimationDataHandler* coreData,QObject* parent)
   : QAbstractListModel(parent)
{
    m_coreData = coreData;
}

FrameModel::~FrameModel()
{
}

int FrameModel::getOverZeroValue(int value)
{
    if(value < 0)
    {
        return 0;
    }
    return value;
}

bool FrameModel::isValidIndex(QModelIndex index) const
{
    Action* action = m_coreData->getCurrentAction();

    if(action == NULL) return false;

    if(!index.isValid() || index.row() >= action->getFrameCount())
    {
        return false;
    }

    return true;
}

QVariant FrameModel::data(const QModelIndex &index, int role) const
{
    Action* action = m_coreData->getCurrentAction();

    if(!isValidIndex(index))
    {
        return QVariant();
    }

    if(role == Qt::DisplayRole || role == Qt::EditRole)
    {
        if(action == NULL)
        {
            return QVariant();
        }

        QString frame = action->getFrameNameByIndex(index.row());
        if(frame == NULL)
        {
            return QVariant();
        }

         return frame;
    }
    else
    {
        return QVariant();
    }
}

Qt::ItemFlags FrameModel::flags(const QModelIndex &index) const
{
    if(!isValidIndex(index))
    {
        return Qt::ItemIsEnabled;
    }
    return QAbstractItemModel::flags(index);
}

QVariant FrameModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
        switch (section) {
            case 0:
                return tr("Name");
            default:
                return QVariant();
        }
    }
    return QVariant();
}

void FrameModel::dataChangeUpdate()
{
    reset();
}

int FrameModel::columnCount(const QModelIndex &parent) const
{
    return  MAX_COLUMN_NUM;
}

int FrameModel::rowCount(const QModelIndex &parent) const
{
    Action* action = m_coreData->getCurrentAction();
    if(action != NULL)
    {
        return action->getFrameCount();
    }
    else
    {
        return 0;
    }
}

QModelIndex FrameModel::getCurrentFrameIndex()
{
    int row = m_coreData->getCurrentActionIndex();
    if(row != -1)
    {
        return this->index(row,0);
    }
    else
    {
        return QModelIndex();
    }
}

QModelIndex FrameModel::getFrameModelIndexbyIndex(int row)
{
    if(row != -1)
    {
        return this->index(row,0);
    }
    else
    {
        return QModelIndex();
    }
}

void FrameModel::appendFrameList(const PlistFrameInfoList &frameList)
{
    Action* action = m_coreData->getCurrentAction();

    if(action == NULL) return;
    int size = action->getFrameCount();
    int newAddSize = frameList.count();


    beginInsertRows(QModelIndex(), size, size);
         m_coreData->appendFrameList(frameList);
    endInsertRows();
}

void FrameModel::removeFrameByIndexList(QModelIndex& index)
{
    Action* action = m_coreData->getCurrentAction();
    if(action == NULL) return;

    beginRemoveRows(QModelIndex(), index.row(),index.row());
         m_coreData->removeFrameByIndex(index.row());
    endRemoveRows();

}

void FrameModel::selectChanged(const QItemSelection &selected, const QItemSelection &desSelected)
{
    QModelIndexList items = selected.indexes();

    if(items.count() > 0)
    {
        QModelIndex index = items.at(0);

        m_coreData->setCurrentFrameByIndex(index.row());

    }
}
